﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace BeatPong
{
    [Serializable]
    public class Beat
    {
        public Beat(TimeSpan b, string e, TimeSpan d)
        {
            beat = b;
            Effect = e;
            EffectDuration = d;
        }

        public Beat(TimeSpan b)
        {
            beat = b;
            Effect = "";
            EffectDuration = new TimeSpan();
        }

        public Beat()
        {
            beat = new TimeSpan();
            EffectDuration = new TimeSpan();
            Effect = "";
        }

        [XmlIgnore]
        public TimeSpan beat
        {
            get;
            set;
        }

        [XmlIgnore]
        public TimeSpan EffectDuration
        {
            get;
            set;
        }

        [XmlAttribute("effect")]
        public string Effect
        {
            get;
            set;
        }
        [XmlAttribute("duration")]
        public string duration
        {
            get { return EffectDuration.ToString(); }
            set 
            {
                try
                {
                    EffectDuration = TimeSpan.Parse(value);
                }
                catch (Exception)
                {
                    EffectDuration = new TimeSpan();
                }
            }
        }

        [XmlText]
        public string beatstr
        {
            get { return beat.ToString(); }
            set
            {
                try
                {
                    beat = TimeSpan.Parse(value);
                }
                catch (Exception)
                {
                    beat = new TimeSpan();
                }
            }
        }
    }
    [Serializable]
    public class GameSong
    {
        [XmlIgnore]
        public TimeSpan length;

        [XmlElement("Beat")]
        public List<Beat> beats;

        [XmlElement("Length", DataType="duration")]
        public string xmllength
        {
            get { return length.ToString(); }
            set { length = TimeSpan.Parse(value); }
        }

        public string AssetName;

        [XmlElement]
        public string Artist;

        [XmlElement]
        public string Title;

        [XmlIgnore]
        public Song song;
    }

    public class SongPlayer
    {
        public static void StartSong(GameSong song)
        {
            MediaPlayer.Play(song.song);            
        }
        public static void StopSong(GameSong song)
        {
            MediaPlayer.Stop();
        }
        public static void PauseSong()
        {
            MediaPlayer.Pause();
        }
        public static void ResumeSong()
        {
            MediaPlayer.Resume();
        }


        public static Beat FindNextBeat(GameSong song)
        {
            TimeSpan fudge = new TimeSpan(0, 0, 0, 0, 125);
            
            foreach (Beat b in song.beats)
            {
                if (b.beat > MediaPlayer.PlayPosition)
                {
                    song.beats.Remove(b);
                    return b;
                }
            }
            return null;
        }

        public static TimeSpan PlayPosition()
        {
            return MediaPlayer.PlayPosition;
        }

        public static bool isPaused()
        {
            return MediaPlayer.State == MediaState.Paused;
        }
    }
}
